package com.casic.modules.jy.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import com.casic.common.utils.R;
import com.casic.common.base.entity.PageEntity;
import com.casic.modules.jy.entity.JySjOptionEntity;
import com.casic.modules.jy.service.JySjOptionService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.casic.modules.jy.entity.JySjTitleEntity;
import com.casic.modules.jy.service.JySjTitleService;


/**
 * 试题信息表
 * 
 * @author mfc
 * @email 123456789@qq.com
 * @date 2018-09-25 14:55:22
 */
@RestController
@RequestMapping("jysjtitle")
public class JySjTitleController {
	@Autowired
	private JySjTitleService jySjTitleService;
	@Autowired
	private JySjOptionService jySjOptionService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
        PageEntity<JySjTitleEntity> page=jySjTitleService.findPage(params);
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") String id){
		JySjTitleEntity jySjTitle = jySjTitleService.findById(id);
		return R.ok().put("jySjTitle", jySjTitle);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody JySjTitleEntity jySjTitle) throws UnsupportedEncodingException {
		jySjTitle.setSjDescribe(URLDecoder.decode(jySjTitle.getSjDescribe(), "utf-8"));
		JySjTitleEntity bean = jySjTitleService.save(jySjTitle);
		return R.ok().put("id",bean.getId());
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody JySjTitleEntity jySjTitle){
		jySjTitleService.edit(jySjTitle);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] ids){
		jySjTitleService.removeByIds(ids);
		return R.ok();
	}

	@RequestMapping("/queryByParam")
	public R queryByParam(@RequestParam Map<String, Object> params){
		List<JySjTitleEntity> list = jySjTitleService.findList(params);
		if(list !=null && list.size()>0){
			for(JySjTitleEntity title : list){
				params.put("titleCode",title.getId());
				List<JySjOptionEntity> optionList = jySjOptionService.findList(params);
				title.setOptionList(optionList);
			}
		}
		return R.ok().put("list", list);
	}


}
